﻿using UnityEngine;
using System.Collections;

public class TileSelection : MonoBehaviour {
	
	//bool wasClicked;
	public GameObject TilePrefab;
	private MatchChecker checker;
	public bool isVisible = false;
	public float pos_shift;	// = 0.1f;
	
	//These coord param are set in the TilesGenerator Distribute() and serve only in the Inspector. No function reads them
	public int i;
	public int j;
	public bool isQuadruple;
	public int matchingTiles;
	private Behaviour halo;
	
	//Constructor to initialize name and position information
	public void Initialize(TilesGenerator _source, string _name, Sprite _sprite,
					float _screenRatio, float _overlap)
	{
		checker = _source.GetComponent<MatchChecker>();
		
		this.GetComponent<SpriteRenderer>().sprite = _sprite; //sprites[Random.Range(0, sprites.Length)] as Sprite;
		//Sprites are 32x40 with 8pixToUnit, we stretch them to match the screen size

		if( 0 != _screenRatio )
			this.transform.localScale = new Vector3(1,_screenRatio,0); 	//width/currentRect.width,_height/currentRect.height
		
		this.transform.name = _name;
		//When the sprite is set, we make the tile visible
		isVisible = true;
		pos_shift = _overlap;
		halo = (this.gameObject.GetComponent("Halo") as Behaviour);

		//0_P4, 8_V4, 9_B9, f_bLambda. Les saisons commencent par _ et les fleurs par -
		//We indicate how many matches the tile has and if it is a quadruple
		switch(name[0])
		{
			case '-':	//Fleurs
				goto case '_';

			case '_':	//Saisons
				isQuadruple = false;
				matchingTiles = 7;
				break;
				
			case '8':
				goto case '0';
				
			case '9':
				goto case '0';
				
			case 'f':
				goto case '0';
			
			case '0':
				isQuadruple = true;
				matchingTiles = 3;
				break;

			default:
				isQuadruple = false;
				matchingTiles = 1;
				break;
		}
	}
	
	public void Prepare(Vector3 _newPos, Transform _newRow)
	{
		this.transform.position = _newPos;
		this.transform.parent = _newRow;
		this.isVisible = true;
		
		// We re-enable the collider as it was probably disabled during the previous game (on tile selection)
		this.collider2D.enabled = true;
		// We DON'T enable the renderer, as this will be done on timer start
		//this.renderer.enabled = true;
	}

	
	void OnMouseDown() {
		//wasClicked = true;
		if( true == isVisible)
			Select();
	}
	
	void OnMouseUp() {
		/*wasClicked = false;
		Unselect();*/
	}
	
	void OnMouseEnter() {

	}
	
	void OnMouseExit() {
		//Unselect();
	}
	
	void Select() {
		//renderer.material.color = Color.red;
		float x = this.gameObject.transform.position.x + pos_shift;
		float y = this.gameObject.transform.position.y - pos_shift;
		float z = this.gameObject.transform.position.z;
		transform.position = new Vector3(x,y, z);
		halo.enabled = true;
		this.gameObject.renderer.material.color = new Color(1,1,1,0.7f);
		collider2D.enabled = false;
		checker.TileSected(this);
	}
	
	public void Unselect() {
		//renderer.material.color = Color.white;
		float x = this.gameObject.transform.position.x - pos_shift;
		float y = this.gameObject.transform.position.y + pos_shift;
		float z = this.gameObject.transform.position.z;
		transform.position = new Vector3(x,y, z);
		halo.enabled = false;
		this.gameObject.renderer.material.color = new Color(1,1,1,1);
		collider2D.enabled = true;
	}

	public void Deactivate()
	{
		renderer.enabled = false;
		collider2D.enabled = false;
		isVisible = false;
		halo.enabled = false;
		renderer.material.color = new Color(1,1,1,1);
	}
	
	public void DeactivateWithAnimation()
	{
		// Same as Deactivate(); but without the sound
		renderer.enabled = false;
		collider2D.enabled = false;
		isVisible = false;
		halo.enabled = false;
		//Then the animation
		StartCoroutine(TileAnimation());
	}
	
	IEnumerator TileAnimation()
	{
		float delta = 0.8f;
		SoundEffectsHelper.Instance.MakeJokerSound();
		while( delta > 0 )
		{
			yield return new WaitForSeconds(delta/8f);
			renderer.enabled = true;
			renderer.material.color = new Color(1,1,1,delta);
			yield return new WaitForSeconds(delta/8f);
			renderer.enabled = false;
			delta -= 0.2f;
		}
		renderer.material.color = new Color(1,1,1,1);
	}
}
